package com.openbravo.pos.ticket;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.LocalRes;
import com.openbravo.data.loader.SerializableRead;
import com.openbravo.format.Formats;
import com.openbravo.pos.customers.CustomerInfoExt;
import com.openbravo.pos.payment.PaymentInfo;
import com.openbravo.pos.payment.PaymentInfoMagcard;
import com.openbravo.pos.util.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

public class TicketInfo
  implements SerializableRead, Externalizable
{
  private static final long serialVersionUID = 2765650092387265178L;
  public static final int RECEIPT_NORMAL = 0;
  public static final int RECEIPT_REFUND = 1;
  public static final int RECEIPT_PAYMENT = 2;
  private static DateFormat m_dateformat = new SimpleDateFormat("hh:mm");
  private String m_sId = UUID.randomUUID().toString();
  private int tickettype = 0;
  private int m_iTicketId = 0;
  private Date m_dDate = new Date();
  private Properties attributes = new Properties();
  private UserInfo m_User = null;
  private CustomerInfoExt m_Customer = null;
  private String m_sActiveCash = null;
  private List<TicketLineInfo> m_aLines = new ArrayList();
  private List<PaymentInfo> payments = new ArrayList();
  private List<TicketTaxInfo> taxes = null;
  private String m_sResponse = null;

  public void writeExternal(ObjectOutput paramObjectOutput)
    throws IOException
  {
    paramObjectOutput.writeObject(this.m_sId);
    paramObjectOutput.writeInt(this.tickettype);
    paramObjectOutput.writeInt(this.m_iTicketId);
    paramObjectOutput.writeObject(this.m_Customer);
    paramObjectOutput.writeObject(this.m_dDate);
    paramObjectOutput.writeObject(this.attributes);
    paramObjectOutput.writeObject(this.m_aLines);
  }

  public void readExternal(ObjectInput paramObjectInput)
    throws IOException, ClassNotFoundException
  {
    this.m_sId = ((String)paramObjectInput.readObject());
    this.tickettype = paramObjectInput.readInt();
    this.m_iTicketId = paramObjectInput.readInt();
    this.m_Customer = ((CustomerInfoExt)paramObjectInput.readObject());
    this.m_dDate = ((Date)paramObjectInput.readObject());
    this.attributes = ((Properties)paramObjectInput.readObject());
    this.m_aLines = ((List)paramObjectInput.readObject());
    this.m_User = null;
    this.m_sActiveCash = null;
    this.payments = new ArrayList();
    this.taxes = null;
  }

  public void readValues(DataRead paramDataRead)
    throws BasicException
  {
    this.m_sId = paramDataRead.getString(1);
    this.tickettype = paramDataRead.getInt(2).intValue();
    this.m_iTicketId = paramDataRead.getInt(3).intValue();
    this.m_dDate = paramDataRead.getTimestamp(4);
    this.m_sActiveCash = paramDataRead.getString(5);
    try
    {
      byte[] arrayOfByte = paramDataRead.getBytes(6);
      if (arrayOfByte != null)
        this.attributes.loadFromXML(new ByteArrayInputStream(arrayOfByte));
    }
    catch (IOException localIOException)
    {
    }
    this.m_User = new UserInfo(paramDataRead.getString(7), paramDataRead.getString(8));
    this.m_Customer = new CustomerInfoExt(paramDataRead.getString(9));
    this.m_aLines = new ArrayList();
    this.payments = new ArrayList();
    this.taxes = null;
  }

  public TicketInfo copyTicket()
  {
    TicketInfo localTicketInfo = new TicketInfo();
    localTicketInfo.tickettype = this.tickettype;
    localTicketInfo.m_iTicketId = this.m_iTicketId;
    localTicketInfo.m_dDate = this.m_dDate;
    localTicketInfo.m_sActiveCash = this.m_sActiveCash;
    localTicketInfo.attributes = ((Properties)this.attributes.clone());
    localTicketInfo.m_User = this.m_User;
    localTicketInfo.m_Customer = this.m_Customer;
    localTicketInfo.m_aLines = new ArrayList();
    Iterator localIterator = this.m_aLines.iterator();
    Object localObject;
    while (localIterator.hasNext())
    {
      localObject = (TicketLineInfo)localIterator.next();
      localTicketInfo.m_aLines.add(((TicketLineInfo)localObject).copyTicketLine());
    }
    localTicketInfo.refreshLines();
    localTicketInfo.payments = new LinkedList();
    localIterator = this.payments.iterator();
    while (localIterator.hasNext())
    {
      localObject = (PaymentInfo)localIterator.next();
      localTicketInfo.payments.add(((PaymentInfo)localObject).copyPayment());
    }
    return (TicketInfo)localTicketInfo;
  }

  public String getId()
  {
    return this.m_sId;
  }

  public int getTicketType()
  {
    return this.tickettype;
  }

  public void setTicketType(int paramInt)
  {
    this.tickettype = paramInt;
  }

  public int getTicketId()
  {
    return this.m_iTicketId;
  }

  public void setTicketId(int paramInt)
  {
    this.m_iTicketId = paramInt;
  }

  public String getName(Object paramObject)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if (getCustomerId() != null)
    {
      localStringBuffer.append(this.m_Customer.toString());
      localStringBuffer.append(" - ");
    }
    if (paramObject == null)
    {
      if (this.m_iTicketId == 0)
        localStringBuffer.append("(" + m_dateformat.format(this.m_dDate) + " " + Long.toString(this.m_dDate.getTime() % 1000L) + ")");
      else
        localStringBuffer.append(Integer.toString(this.m_iTicketId));
    }
    else
      localStringBuffer.append(paramObject.toString());
    return localStringBuffer.toString();
  }

  public String getName()
  {
    return getName(null);
  }

  public Date getDate()
  {
    return this.m_dDate;
  }

  public void setDate(Date paramDate)
  {
    this.m_dDate = paramDate;
  }

  public UserInfo getUser()
  {
    return this.m_User;
  }

  public void setUser(UserInfo paramUserInfo)
  {
    this.m_User = paramUserInfo;
  }

  public CustomerInfoExt getCustomer()
  {
    return this.m_Customer;
  }

  public void setCustomer(CustomerInfoExt paramCustomerInfoExt)
  {
    this.m_Customer = paramCustomerInfoExt;
  }

  public String getCustomerId()
  {
    if (this.m_Customer == null)
      return null;
    return this.m_Customer.getId();
  }

  public String getTransactionID()
  {
    return getPayments().size() > 0 ? ((PaymentInfo)getPayments().get(getPayments().size() - 1)).getTransactionID() : StringUtils.getCardNumber();
  }

  public String getReturnMessage()
  {
    return (getPayments().get(getPayments().size() - 1) instanceof PaymentInfoMagcard) ? ((PaymentInfoMagcard)(PaymentInfoMagcard)getPayments().get(getPayments().size() - 1)).getReturnMessage() : LocalRes.getIntString("button.ok");
  }

  public void setActiveCash(String paramString)
  {
    this.m_sActiveCash = paramString;
  }

  public String getActiveCash()
  {
    return this.m_sActiveCash;
  }

  public String getProperty(String paramString)
  {
    return this.attributes.getProperty(paramString);
  }

  public String getProperty(String paramString1, String paramString2)
  {
    return this.attributes.getProperty(paramString1, paramString2);
  }

  public void setProperty(String paramString1, String paramString2)
  {
    this.attributes.setProperty(paramString1, paramString2);
  }

  public Properties getProperties()
  {
    return this.attributes;
  }

  public TicketLineInfo getLine(int paramInt)
  {
    return (TicketLineInfo)this.m_aLines.get(paramInt);
  }

  public void addLine(TicketLineInfo paramTicketLineInfo)
  {
    paramTicketLineInfo.setTicket(this.m_sId, this.m_aLines.size());
    this.m_aLines.add(paramTicketLineInfo);
  }

  public void insertLine(int paramInt, TicketLineInfo paramTicketLineInfo)
  {
    this.m_aLines.add(paramInt, paramTicketLineInfo);
    refreshLines();
  }

  public void setLine(int paramInt, TicketLineInfo paramTicketLineInfo)
  {
    paramTicketLineInfo.setTicket(this.m_sId, paramInt);
    this.m_aLines.set(paramInt, paramTicketLineInfo);
  }

  public void removeLine(int paramInt)
  {
    this.m_aLines.remove(paramInt);
    refreshLines();
  }

  private void refreshLines()
  {
    for (int i = 0; i < this.m_aLines.size(); i++)
      getLine(i).setTicket(this.m_sId, i);
  }

  public int getLinesCount()
  {
    return this.m_aLines.size();
  }

  public double getArticlesCount()
  {
    double d = 0.0D;
    Iterator localIterator = this.m_aLines.iterator();
    while (localIterator.hasNext())
    {
      TicketLineInfo localTicketLineInfo = (TicketLineInfo)localIterator.next();
      d += localTicketLineInfo.getMultiply();
    }
    return d;
  }

  public double getSubTotal()
  {
    double d = 0.0D;
    Iterator localIterator = this.m_aLines.iterator();
    while (localIterator.hasNext())
    {
      TicketLineInfo localTicketLineInfo = (TicketLineInfo)localIterator.next();
      d += localTicketLineInfo.getSubValue();
    }
    return d;
  }

  public double getTax()
  {
    double d = 0.0D;
    Iterator localIterator;
    Object localObject;
    if (hasTaxesCalculated())
    {
      localIterator = this.taxes.iterator();
      while (localIterator.hasNext())
      {
        localObject = (TicketTaxInfo)localIterator.next();
        d += ((TicketTaxInfo)localObject).getTax();
      }
    }
    else
    {
      localIterator = this.m_aLines.iterator();
      while (localIterator.hasNext())
      {
        localObject = (TicketLineInfo)localIterator.next();
        d += ((TicketLineInfo)localObject).getTax();
      }
    }
    return d;
  }

  public double getTotal()
  {
    return getSubTotal() + getTax();
  }

  public double getTotalPaid()
  {
    double d = 0.0D;
    Iterator localIterator = this.payments.iterator();
    while (localIterator.hasNext())
    {
      PaymentInfo localPaymentInfo = (PaymentInfo)localIterator.next();
      if (!"debtpaid".equals(localPaymentInfo.getName()))
        d += localPaymentInfo.getTotal();
    }
    return d;
  }

  public List<TicketLineInfo> getLines()
  {
    return this.m_aLines;
  }

  public void setLines(List<TicketLineInfo> paramList)
  {
    this.m_aLines = paramList;
  }

  public List<PaymentInfo> getPayments()
  {
    return this.payments;
  }

  public void setPayments(List<PaymentInfo> paramList)
  {
    this.payments = paramList;
  }

  public void resetPayments()
  {
    this.payments = new ArrayList();
  }

  public List<TicketTaxInfo> getTaxes()
  {
    return this.taxes;
  }

  public boolean hasTaxesCalculated()
  {
    return this.taxes != null;
  }

  public void setTaxes(List<TicketTaxInfo> paramList)
  {
    this.taxes = paramList;
  }

  public void resetTaxes()
  {
    this.taxes = null;
  }

  public TicketTaxInfo getTaxLine(TaxInfo paramTaxInfo)
  {
    Iterator localIterator = this.taxes.iterator();
    while (localIterator.hasNext())
    {
      TicketTaxInfo localTicketTaxInfo = (TicketTaxInfo)localIterator.next();
      if (paramTaxInfo.getId().equals(localTicketTaxInfo.getTaxInfo().getId()))
        return localTicketTaxInfo;
    }
    return new TicketTaxInfo(paramTaxInfo);
  }

  public TicketTaxInfo[] getTaxLines()
  {
    HashMap localHashMap = new HashMap();
    Object localObject = this.m_aLines.iterator();
    while (((Iterator)localObject).hasNext())
    {
      TicketLineInfo localTicketLineInfo = (TicketLineInfo)((Iterator)localObject).next();
      TicketTaxInfo localTicketTaxInfo = (TicketTaxInfo)localHashMap.get(localTicketLineInfo.getTaxInfo().getId());
      if (localTicketTaxInfo == null)
      {
        localTicketTaxInfo = new TicketTaxInfo(localTicketLineInfo.getTaxInfo());
        localHashMap.put(localTicketTaxInfo.getTaxInfo().getId(), localTicketTaxInfo);
      }
      localTicketTaxInfo.add(localTicketLineInfo.getSubValue());
    }
    localObject = localHashMap.values();
    int size = ((Collection)localObject).size();
	TicketTaxInfo[] ticketTaxInfos = new TicketTaxInfo[size];
	return  (TicketTaxInfo[])((Collection)localObject).toArray(ticketTaxInfos);
  }

  public String printId()
  {
    if (this.m_iTicketId > 0)
      return Formats.INT.formatValue(new Integer(this.m_iTicketId));
    return "";
  }

  public String printDate()
  {
    return Formats.TIMESTAMP.formatValue(this.m_dDate);
  }

  public String printUser()
  {
    return this.m_User == null ? "" : this.m_User.getName();
  }

  public String printCustomer()
  {
    return this.m_Customer == null ? "" : this.m_Customer.getName();
  }

  public String printArticlesCount()
  {
    return Formats.DOUBLE.formatValue(new Double(getArticlesCount()));
  }

  public String printSubTotal()
  {
    return Formats.CURRENCY.formatValue(new Double(getSubTotal()));
  }

  public String printTax()
  {
    return Formats.CURRENCY.formatValue(new Double(getTax()));
  }

  public String printTotal()
  {
    return Formats.CURRENCY.formatValue(new Double(getTotal()));
  }

  public String printTotalPaid()
  {
    return Formats.CURRENCY.formatValue(new Double(getTotalPaid()));
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.ticket.TicketInfo
 * JD-Core Version:    0.6.0
 */